A class, in object-oriented programming (OOP) technology, is a user-defined data type that defines a collection of objects that share the same characteristics. An object, or class member, is one instance of the class. Concrete classes are designed to be instantiated. Abstract classes are designed to pass on characteristics through inheritance.
Classes can be used in conjunction with virtual machines, where a virtual machine here means an execution environment for a programming language. For instance, a Java® virtual machine (JVM) is software that converts Java® intermediate language code (bytecode) into machine language and executes it. A JVM is commonly installed in a web server to execute server-side Java® programs, and can also be installed in a client machine to run stand-alone Java® applications.
A classloader is software that executes as part of a virtual machine. A classloader finds a class file defining a class and that is stored on non-volatile storage like a hard disk drive, reads it into memory, and tells the virtual machine to treat the memory image of the class as legitimate bytecode. In loading a class or classes, a classloader thus needs memory to store information for the class or classes loaded.
Therefore, memory segments are allocated for each classloader, in which the information for the classes loaded by a given classloader is stored. A first memory segment is allocated for a given classlbader within the prior art that has a fixed size, regardless of the information to be stored in that memory segment by the classloader. If there is insufficient room within the memory segment to store all the information, another segment of the same fixed size is allocated within the prior art. This approach allows for fast unloading of classes later. Unloading occurs on a per-classloader basis in some types of virtual machines, such as the JVM, such that the memory allocated to a classloader can be quickly freed by deallocating the memory segments allocated to the classloader.
However, in some types of VM environments, such as some types of JVM environments, many classloaders are created. This results in significant waste of memory resources, since much of the memory allocated to the classloaders is not actually used to store class information. That is, memory allocated but not used wastes memory resources.
FIG. 1 shows an example of class memory management in accordance with the fixed-size strategy of the prior art. For a first classloader, a memory segment 102A is initially allocated having a fixed size corresponding to the width of the memory segment 102A in FIG. 1, such as 128 kilobytes (KB). Class information 104A is first stored by the first classloader within the memory segment 102A. Thereafter, class information 104B, 104C, and 104D are successively stored by the first classloader within the memory segment 102A. The sizes of the class information 104A, 104B, 104C, and 104D are proportional to their width relative to the width of the memory segment 102A itself. The portion of the memory segment 102A that remains free is the unused portion 106A, and has a size proportional to its width relative to the width of the memory segment 102A itself.
The next class information to be stored by the first classloader is the class information 104E. However, the class information 104E is greater in size than the unused portion 106A of the memory segment 102A. That is, as shown in FIG. 1, the width of class information 104E is wider than the width of the unused portion 106A. Therefore, another memory segment 102B is allocated to the first classloader, and which has a fixed size that is equal to the fixed size of the memory segment 102A. Thus, the widths of the memory segments 102A and 102B are the same.
The class information 104E is then stored in the memory segment 104B. Thereafter, class information 104F, 104G, 104H, and 1041 are successively stored by the first classloader within the memory segment 102B since their sizes are larger than 106A. The sizes of the class information 104E, 104F, 104G, 104H, and 1041 are proportional to their widths relative to the width of the memory segment 102B itself. The portion of the memory segment 102B that remains free is the unused portion 106B, and has a size proportional to its width relative to the width of the memory segment 102B itself.
The final class information to be stored by the first classloader is the class information 104J and the class information 104K. However, the class information 104J is greater in size than the unused portion 106B of the memory segment 102B and 106A of 102A. That is, as shown in FIG. 1, the width of the class information 104J is wider than the width of the unused portions 106B and 106A. Therefore, another memory segment 102C is allocated to the first classloader, and which has a fixed size that is equal to the fixed size of the memory segments 102A and 102B. Thus, the widths of the memory segments 102A, 102B, and 102C are the same.
The class information 104J is then stored in the memory segment 102C, and thereafter the class information 104K is stored in the memory segment 102C. The portion of the memory segment 102C that remains free is the unused portion 106C, which has a size proportional to its width relative to the width of the memory segment 102C. Thus, to store all the class information 104A, 104B, 104C, 104D, 104E, 104F, 104G, 104H, 1041, 104J, and 104K, the first classloader has had allocated to it three memory segments 102A, 102B, and 102C. However, unused portions 106A, 106B, and 106C of the memory segments 102A, 102B, and 102C remain, meaning that these portions of memory are wasted.
A second classloader has allocated to it a memory segment 108 having a fixed size that is also equal to the sizes of the memory segments 102A, 102B, and 102C allocated to the first classloader. Thus, the widths of the memory segments 102A, 102B, 102C, and 108 are the same. The second classloader just has to store one piece of class information 110. As a result, the portion of the memory segment 108 that remains free is the unused portion 112, which has a size proportional to its width relative to the width of the memory segment 108. Therefore, the unused portion 112 is also wasted, because no classes will be loaded by the second classloader.
The fixed size memory segment allocation scheme of the prior art, therefore, is wasteful. For classloaders that load just one or a few classes and thus store just a little class information, like the second classloader in FIG. 2, the prior art scheme can be inefficient, where the size of the memory segment allocated is much greater than the amount of memory needed to store the class information in question.
For these and other reasons, therefore, there is a need for the present invention.